#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
IR 表达式语句节点定义
"""

import ast
from dataclasses import dataclass, field
from typing import Iterator
from .base import IRStmt
from ..base import IRNode, get_default_ir_node
from ...symbol_table import Symbol
from ..expressions import build_expr_from_ast


@dataclass
class IRExprStmt(IRStmt):
    """表达式语句"""

    expr: IRNode = field(default_factory=get_default_ir_node)
    node_type: str = "expr_stmt"

    def get_child_nodes(self) -> Iterator[IRNode]:
        """获取所有子节点"""
        yield self.expr
        yield from super().get_child_nodes()

    def build_from_ast(self, node: ast.Expr) -> None:
        """从 AST 表达式节点构建 IR 表达式语句节点内容

        Args:
            node: AST 表达式节点
        """
        super().build_from_ast(node)

        self.expr = build_expr_from_ast(node.value, self)
